﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Controls.ReportSVC;
using SMS.Common.Silverlight;
using System.Collections.ObjectModel;
using SMS.Entities;
using SMS.Entities.Report;

namespace SMS.Model
{
    public class ReportModel
    {
        ServiceProxy<ReportServiceClient> proxy = null;
        public event Action<ObservableCollection<SaleReport>> SaleDetailReportDataCompleted;
        public event Action<ObservableCollection<ProductSaleReport>> OnProductSaleReportDataCompleted;
        public event Action<ObservableCollection<CustomerReport>> OnCustomerReportDataCompleted;
        public event Action<ObservableCollection<NewCustomerReport>> OnNewCustomerReportDataCompleted;
        public event Action<SMSException> OnServiceError;
        public ReportModel()
        {
            proxy = new ServiceProxy<ReportServiceClient>("Service/ReportService.svc");
            proxy.Client.GetSaleDetailReportListCompleted += new EventHandler<GetSaleDetailReportListCompletedEventArgs>(Client_GetSaleDetailReportListCompleted);
            proxy.Client.GetProductSaleReportDataListCompleted += new EventHandler<GetProductSaleReportDataListCompletedEventArgs>(Client_GetProductSaleReportDataListCompleted);
            proxy.Client.GetCustomerReportDataListCompleted += new EventHandler<GetCustomerReportDataListCompletedEventArgs>(Client_GetCustomerReportDataListCompleted);
            proxy.Client.GetIndividualCustomerReportDataListCompleted += new EventHandler<GetIndividualCustomerReportDataListCompletedEventArgs>(Client_GetIndividualCustomerReportDataListCompleted);
            proxy.Client.GetNewCustomerReportDataListCompleted += new EventHandler<GetNewCustomerReportDataListCompletedEventArgs>(Client_GetNewCustomerReportDataListCompleted);
        }

        void Client_GetNewCustomerReportDataListCompleted(object sender, GetNewCustomerReportDataListCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnNewCustomerReportDataCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_GetIndividualCustomerReportDataListCompleted(object sender, GetIndividualCustomerReportDataListCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnCustomerReportDataCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_GetCustomerReportDataListCompleted(object sender, GetCustomerReportDataListCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnCustomerReportDataCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }        

        void Client_GetProductSaleReportDataListCompleted(object sender, GetProductSaleReportDataListCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnProductSaleReportDataCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_GetSaleDetailReportListCompleted(object sender, GetSaleDetailReportListCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                SaleDetailReportDataCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        public void GetSaleDetailReportData(string userName, DateTime FromDate,DateTime ToDate,string SearchOption)
        {
            proxy.Client.GetSaleDetailReportListAsync(userName, FromDate,ToDate,SearchOption);
        }

        public void GetProductSaleDetailReportData(string userName, DateTime FromDate, DateTime ToDate, string SearchOption)
        {
            proxy.Client.GetProductSaleReportDataListAsync(userName, FromDate, ToDate, SearchOption);
        }
        public void GetCustomerDetailReportData(string userName, DateTime FromDate, DateTime ToDate, string SearchOption)
        {
            proxy.Client.GetCustomerReportDataListAsync(userName, FromDate, ToDate, SearchOption);
        }
        public void GetIndividualCustomerDetailReportData(string userName, int CustomerID, DateTime FromDate, DateTime ToDate, string SearchOption)
        {
            proxy.Client.GetIndividualCustomerReportDataListAsync(userName,CustomerID, FromDate, ToDate, SearchOption);
        }
        public void GetNewCustomerDetailReportData(string userName, DateTime FromDate, DateTime ToDate, string SearchOption)
        {
            proxy.Client.GetNewCustomerReportDataListAsync(userName, FromDate, ToDate, SearchOption);
        }
    }
}
